<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目">
              <a-input placeholder="请输入项目" v-model="queryParam.projectName" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料描述">
              <a-input placeholder="请输入物料描述" v-model="queryParam.materialDesc"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :span="8">
              <a-form-item label="需求日期">
                <j-date placeholder="开始日期" class="query-group-cust" v-model="queryParam.reqDateBegin"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date placeholder="结束日期" class="query-group-cust" v-model="queryParam.reqDateEnd"></j-date>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="单据编号">
                <a-input placeholder="请输入单据编号" v-model="queryParam.reqNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="  申请人">
                <j-search-select-tag placeholder="请选择申请人" v-model="queryParam.applyEmpId" dict="sys_user,realname,id"  />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="申请类型">
                <j-dict-select-tag placeholder="请选择申请类型" v-model="queryParam.reqType" dictCode="mm_purchase_reqtype"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-has="'purReq:add'" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('采购申请')" v-has="'purReq:exp'">导出</a-button>
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'purReq:delBatch'"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <div  style="float: right">
          <a-checkbox style="color: #1890FF;margin-right: 12px" :checked="sameCheck" @change="sameCheckChang">同单据批量选中</a-checkbox>
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row>
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <a-col :span="6"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />自定义列</a>
          </a-popover>
        </div>
      </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onSelect: onSelect, onSelectAll:onSelectAll}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="materialDesc" slot-scope="text, record">
          <j-ellipsis :value="text" :length="18"/>
        </template>
        <template slot="materialNo" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <template slot="projectId_dictText" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <template slot="reqNo" slot-scope="text, record">
          <a @click="handleDetail(record)">{{text}}</a>
        </template>
        <template slot="status_dictText" slot-scope="text, record">
          <a-tag :color="getStatusColor(record.status)">
            {{text}}
          </a-tag>
        </template>
        <template slot="uses" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <template slot="remark" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
          <span v-has="'purReq:edit'">
            <a-divider type="vertical" />
            <a @click="handleEdit(record)">编辑</a>
          </span>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item  v-has="'purReq:print'">
                <a @click="handlePrint(record)">打印</a>
              </a-menu-item>
              <a-menu-item  v-has="'purReq:del'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <mm-purchase-requisition-modal ref="modalForm" @ok="modalFormOk"/>
    <mm-purchase-requisition-modal-drawer ref="modalDetail" @ok="modalDetailOk"/>
    <yq-print-modal ref="modalPrint" report-id="817645652416442368"/>
  </a-card>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MmPurchaseRequisitionModal from './modules/MmPurchaseRequisitionModal'
  import MmPurchaseRequisitionModalDrawer from './modules/MmPurchaseRequisitionModal.Style#Drawer'
  import YqPrintModal from '@/views/slf/components/print/YqPrintModal'
  import '@/assets/less/TableExpand.less'

  export default {
    name: "MmPurchaseRequisitionDetailList",
    mixins:[JeecgListMixin],
    components: {
      MmPurchaseRequisitionModal,
      YqPrintModal,
      MmPurchaseRequisitionModalDrawer,
    },
    data () {
      return {
        description: '采购申请管理页面',
        // 表头
        columns: [
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'reqNo',
            scopedSlots: { customRender: 'reqNo' },
            width: 130
          },
          {
            title: '行号',
            align:"center",
            dataIndex: 'lineNo',
            width: 70
          },
          {
            title: '项目',
            align:"center",
            dataIndex: 'projectId_dictText',
            scopedSlots: { customRender: 'projectId_dictText' },
            width: 200
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialNo',
            scopedSlots: { customRender: 'materialNo' },
            width: 130
          },
          {
            title:'物料描述',
            align:"center",
            dataIndex: 'materialDesc',
            scopedSlots: { customRender: 'materialDesc' },
            width: 240
          },
          {
            title: '单位',
            align:"center",
            dataIndex: 'unit_dictText',
            width: 70
          },
          {
            title: '申请数量',
            align:"center",
            dataIndex: 'reqQty',
            width: 110
          },
          {
            title: '采购数量',
            align:"center",
            dataIndex: 'purQty',
            width: 110
          },
          {
            title: '未采数量',
            align:"center",
            dataIndex: 'unpurQty',
            width: 110
          },
          {
            title: '状态',
            align:"center",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'status_dictText' },
            width: 70
          },
          {
            title:'需求日期',
            align:"center",
            dataIndex: 'reqDate',
            width: 100,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '用途',
            align:"center",
            dataIndex: 'uses',
            scopedSlots: { customRender: 'uses' },
            width: 140
          },
          {
            title:'备注',
            align:"center",
            scopedSlots: { customRender: 'remark' },
            dataIndex: 'remark',
            width: 140
          },
          {
            title:'申请类型',
            align:"center",
            dataIndex: 'reqType_dictText',
            width: 100
          },
          {
            title:'采购员',
            align:"center",
            dataIndex: 'buyer_dictText',
            width: 70
          },
          {
            title:'申请人',
            align:"center",
            dataIndex: 'applyEmpId_dictText',
            width: 70
          },
          {
            title:'申请部门',
            align:"center",
            dataIndex: 'applyDeptId_dictText',
            width: 100
          },
          {
            title:'申请日期',
            align:"center",
            dataIndex: 'applyDate',
            width: 100,
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy_dictText',
            width: 70
          },
          {
            title:'创建部门',
            align:"center",
            dataIndex: 'sysOrgCode_dictText',
            width: 100
          },
          {
            title:'创建日期',
            align:"center",
            dataIndex: 'createTime',
            width: 100,
            customRender:function (text) {
              return !text?"":(text.length>19?text.substr(0,19):text)
            }
          },
          {
            title:'修改人',
            align:"center",
            dataIndex: 'updateBy_dictText',
            width: 70
          },
          {
            title:'修改日期',
            align:"center",
            dataIndex: 'updateTime',
            width: 100,
            customRender:function (text) {
              return !text?"":(text.length>19?text.substr(0,19):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: {
              customRender: 'action',
            },
          }
        ],
        url: {
          list: "/mm/purchaseRequisition/pageDetailList",
          detailList: "/mm/purchaseRequisition/detailList",
          delete: "/mm/purchaseRequisition/deleteDetail",
          deleteBatch: "/mm/purchaseRequisition/deleteBatchDetail",
          exportXlsUrl: "/mm/purchaseRequisition/exportXls",
          importExcelUrl: "mm/purchaseRequisition/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
        sameCheck: false,
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
         fieldList.push({type:'string',value:'reqNo',text:'单据编号',dictCode:''})
         fieldList.push({type:'string',value:'reqType',text:'申请类型',dictCode:'mm_purchase_reqtype'})
         fieldList.push({type:'string',value:'factoryId',text:'工厂',dictCode:'mdm_factory,factory_name,id'})
         fieldList.push({type:'date',value:'reqDate',text:'需求日期'})
         fieldList.push({type:'BigDecimal',value:'totalPrice',text:'预估总价',dictCode:''})
         fieldList.push({type:'sel_user',value:'applyEmpId',text:'申请人'})
         fieldList.push({type:'string',value:'applyDeptId',text:'申请部门',dictCode:''})
         fieldList.push({type:'string',value:'status',text:'状态',dictCode:'mm_purchase_reqstatus'})
         fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      },
      getStatusColor(value){
        if(value === '01'){
          // 申请
          return 'blue';
        }else if(value === '02'){
          // 采购
          return 'red';
        }else if(value === '03'){
          // 完成
          return 'green';
        }else {
          // 关闭
          return '#C0C0C0';
        }
      },
      handlePrint(record){
        this.$refs.modalPrint.print(record.pid);
      },
      handleDetail(record){
        this.$refs.modalDetail.edit(record);
        this.$refs.modalDetail.title="详情";
        this.$refs.modalDetail.disableSubmit = true;
      },
      modalDetailOk() {
        this.loadData();
        this.onClearSelected()
      },
      batchPrint(){
      },
      sameCheckChang(e){
        this.sameCheck = e.target.checked;
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>